package com.soecode.lyf.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Collect;
import com.soecode.lyf.service.RecordService;

@Controller
@RequestMapping("/Record") // url:/模块/资源/{id}/细分 /seckill/list
public class RecordController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RecordService recordservice;
	
	@ResponseBody
	@RequestMapping(value="/recordlist", method = RequestMethod.GET)
	public Result recordList(String userid,String jcname, Model model) {
		Result result = new Result();
		if(StringUtils.isEmpty(jcname)&&!StringUtils.isEmpty(userid)) { // 查询阅读记录
		List<Collect> list = recordservice.selectRecord(userid);
		model.addAttribute("RECORDLIST",list);
		result =new Result(0,model,"查询成功");
		}else if(!StringUtils.isEmpty(jcname)&&!StringUtils.isEmpty(userid)){
		int i = recordservice.deleteRecord(userid,jcname);
		if(i==0) { // 删除失败
			result = new Result(1,"删除学习记录失败");
		}else { // 删除成功
			result = new Result(0,"删除学习记录成功");
		}
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/record", method = RequestMethod.GET)
	public Result addCollection(String imgpath, String imgtext, String imghref, String router, String userid,String time,String jcread) {
		Result result = new Result();
		if(!StringUtils.isEmpty(imgpath)&&!StringUtils.isEmpty(imgtext)&&!StringUtils.isEmpty(imghref)&&!StringUtils.isEmpty(router)&&!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(time)&&!StringUtils.isEmpty(jcread)) {
			Collect recordExist = isExist(imgtext,userid);
			if(StringUtils.isEmpty(recordExist)) { // 如果没有记录，则插入
			int i = recordservice.addRecord(imgpath,imgtext,imghref,router,userid,time,jcread);
			if(i==0) {
			result = new Result(1,"记录失败");	
			}else {
			result = new Result(0,"记录成功");
			}
			}else { // 如果有记录，则只更新时间字段
				Date time1 = new Date();
		        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		        String thistime = df.format(time1);
			int i = recordservice.updateRecord(imgtext,userid,thistime);	
				System.out.println("更新时间");
			}
		}
		return result;
	}
	public Collect isExist(String imgtext,String userid) { // 判断用户是否在数据库中存在
		Collect col = recordservice.SelectRecorded(imgtext, userid);
		return col;
	}
	@ResponseBody
	@RequestMapping(value="/progress", method = RequestMethod.GET)
	public Result updateProgress(String userid, String progresstext, String progress, String progressarr) {
		Result result = new Result();
		if(!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(progresstext)&&!StringUtils.isEmpty(progress)&&!StringUtils.isEmpty(progressarr)) {
			int updateprocess = recordservice.updateProgress(userid,progresstext,progress,progressarr);
			int updateexperience = recordservice.updateExperience(userid);
			if(updateprocess!=0&&updateexperience!=0) {
			result = new Result(0,"更新成功");	
			}
		}
		return result;
	}
}
